home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 002 / nywmap.arc / KEYCOMP.C next >
Encoding:
C/C++ Source or Header  |  1986-11-11  |  4.7 KB  |  199 lines

  1. /******************************************************************************
  2.  
  3.  KEYCOMP.C
  4.    Reads a file (arg1) whose line are of the form
  5.      key=func
  6.  
  7.    Key can be a decimal number, 'char', ^char (control char),
  8.    F# (normal func key), AF# (ALT func key), SF# (shift func key),
  9.    CF# (CTRL func key), @char (ALT key)
  10.  
  11.    Func can be a decimal number or a character of the form 'c' or c.
  12.  
  13.    This program will generate a 256 byte "compiled" keystroke file called
  14.    KEYTABLE.NYW. Byte n will contain the translated char that will be read
  15.    by ngetc() when key n is pressed.
  16.  
  17. ******************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <fcntl.h>
  22. #include "keys.h"
  23.  
  24. /*** BASE VALUES FOR FUNCTION KEY 0 ***/
  25. #define NORM_FUNC   186
  26. #define SHIFT_FUNC  211
  27. #define CTRL_FUNC   221
  28. #define ALT_FUNC    231
  29.  
  30. typedef struct keymap
  31. {
  32.   struct keymap *nextmap;
  33.   int    prefix;
  34.   unsigned char keytable[256];
  35. } KEYMAP;
  36.  
  37. struct _alts
  38. {
  39.   char *altchars;
  40.   int  altbase;
  41. } alts[5] =
  42. {
  43.   "QWERTYUIOP", 144,
  44.   "ASDFGHJKL",  158,
  45.   "ZXCVBNM",    172,
  46.   "12345678",   248,
  47.   "90-+",    128
  48. };
  49.  
  50.  
  51. KEYMAP *alloc_keymap(),
  52.        *find_keymap();
  53. KEYMAP *MainKeyMap;
  54. int    NumMaps = 0;
  55.  
  56.  
  57. main(argc, argv)
  58.   int  argc;
  59.   char **argv;
  60. {
  61.   char *fgets();
  62.   FILE *f, *fopen();
  63.   unsigned char buf[100], c;
  64.   char *s, *t, *strrchr();
  65.   char *kp;
  66.   int  i, k, outf;
  67.   KEYMAP *kmap, *lastmap;
  68.   long magic;
  69.  
  70.   int  debug = 1;
  71.  
  72.   if (argc < 2)
  73.     f = stdin;
  74.   else if ((f = fopen(argv[1], "r")) == NULL)
  75.   {
  76.     fprintf(stderr, "Can't open input file %s\n", argv[1]);
  77.     exit(1);
  78.   }
  79.  
  80.   /* init the table with byte[n] = n */
  81.   MainKeyMap = kmap = lastmap = alloc_keymap(0);
  82.   for (kp = MainKeyMap->keytable, i = 0;  i < 256;  *kp++ = i++)  ;
  83.  
  84.   while (fgets(buf, 100, f))
  85.   {
  86.     /* skip any comments and blank lines */
  87.     if (*buf == '#' || isspace(*buf))
  88.       continue;
  89.  
  90.     /* convert left half to all upper case (if not in quotes) */
  91.     for (t = buf;  *t && *t != '\'';  t++)
  92.       *t = toupper(*t);
  93.  
  94.     if (!strncmp(buf, "PREFIX", 6))
  95.     {
  96.       for (s = buf+6;  *s && isspace(*s);  s++)  ;
  97.  
  98.       k = decode_key(s);
  99.       MainKeyMap->keytable[k] = KEYPREFIX;
  100.       kmap = alloc_keymap(k);
  101.       lastmap->nextmap = kmap;
  102.       lastmap = kmap;
  103.       continue;
  104.     }
  105.  
  106.     /* Separate left and right hand sides */
  107.     if ((s = strrchr(buf, '=')) == NULL)
  108.       continue;
  109.     else
  110.       *s = '\0';
  111.  
  112.     if ((k = decode_key(buf)) >= 0)    /* fill in the entry */
  113.     {
  114.       kmap->keytable[k] = c = decode_key(s+1);
  115.       if (isalpha(k) &&        /* Upper & lower case entries should be same */
  116.       kmap != MainKeyMap)  /*  in prefix commands (ie ^KD == ^Kd).      */
  117.     kmap->keytable[toupper(k)] = c;
  118.     }
  119.   }
  120.  
  121.   /* Write out the keymaps */
  122.   if ((outf = open("keytable.nyw", O_CREAT|O_TRUNC|O_WRONLY|O_RAW, 0)) >= 0)
  123.   {
  124.     magic = KEYMAGIC;
  125.     write(outf, &magic, sizeof(magic));
  126.     write(outf, &NumMaps, sizeof(NumMaps));
  127.     for (kmap = MainKeyMap;  kmap;  kmap = kmap->nextmap)
  128.     {
  129.       write(outf, &kmap->prefix, sizeof(kmap->prefix));
  130.       write(outf, kmap->keytable, sizeof(char) * 256);
  131.     }
  132.   }
  133. }
  134.  
  135.  
  136. decode_key(buf)
  137.   char *buf;
  138. {
  139.   int  c, k;
  140.  
  141.   if (isdigit(c = buf[0]))         /* numerical key */
  142.     k = atoi(buf);
  143.   else if (c == '@')             /* ALT keystroke */
  144.     k = find_alt(buf[1]);
  145.   else if (c == '\'')                /* quoted char  */
  146.     k = buf[1];
  147.   else if (c == '^')             /* control char */
  148.     k = buf[1] & 0x1F;
  149.   else if (c == 'F')             /* regular function key */
  150.     k = NORM_FUNC + atoi(buf+1);
  151.   else if (buf[1] == 'F')
  152.   {
  153.     if (c == 'A')             /* AF = ALT func key */
  154.       k = ALT_FUNC;
  155.     else if (c == 'S')           /* SF = SHIFT func key */
  156.       k = SHIFT_FUNC;
  157.     else if (c == 'C')           /* CF - CTRL func key */
  158.       k = CTRL_FUNC;
  159.     k += atoi(buf+2);             /* should be '1' - '10' */
  160.   }
  161.   else
  162.     k = c;
  163.  
  164.   if (isalpha(k) && k < 127)  k = tolower(k);
  165.  
  166.   return(k);
  167. }
  168.  
  169. /* find_alt - takes care translating of '@char' sequence */
  170. find_alt(c)
  171.   char c;
  172. {
  173.   int  i;
  174.   char *s, *strchr();
  175.  
  176.   /* search the string in alt[i] for the char - if it's in, return */
  177.   /* the base value plus its position in the string.           */
  178.   for (i = 0;  i < 5;  i++)
  179.     if ((s = strchr(alts[i].altchars, c)) != NULL)
  180.       return (int) (alts[i].altbase + (s - alts[i].altchars));
  181.   return 0;
  182. }
  183.  
  184. KEYMAP *alloc_keymap(prefix)
  185.   unsigned prefix;
  186. {
  187.    KEYMAP *k;
  188.    char   *calloc();
  189.  
  190.    k = (KEYMAP *) calloc(sizeof(KEYMAP), 1);
  191.    k->nextmap = NULL;
  192.    k->prefix = prefix;
  193.    NumMaps++;
  194.    return(k);
  195. }
  196.  
  197.  
  198.  
  199.